FROM daocloud.io/library/ubuntu:18.04
ARG SOLC=0.5.12

# change apt-get environment
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse" > /etc/apt/sources.list  \
&& echo "# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list    \
&& echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list    \
&& echo "# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list    \
&& echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list    \
&& echo "# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list    \
&& echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list    \
&& echo "# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list    \
&& echo "# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list    \
&& echo "# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list 
# install basic packages
RUN apt-get update && apt-get install -y\
    software-properties-common\
    locales

# set correct locale
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

# install tools
RUN apt-get update &&\
        apt-get -y install\
        wget\
        gdebi

# install souffle
RUN wget https://dl.serctl.com/downloads2/2020-07-26-02-56-15-download-souffle_1.6.2-1_amd64.deb -O /tmp/souffle.deb &&\
        gdebi --n /tmp/souffle.deb

# install graphviz and pip
RUN apt-get update && apt-get -y install\
        graphviz \
        python3.7 \
        python3-pip \
	curl

# install the required solc vesion
RUN curl -L https://dl.serctl.com/downloads2/2020-07-26-03-00-58-download-solc-static-linux > /usr/bin/solc-$SOLC && \
    chmod +x /usr/bin/solc-$SOLC && \
    ln -s /usr/bin/solc-$SOLC /usr/local/bin/solc

COPY requirements.txt /requirements.txt

WORKDIR /sec

# copy and compile securify
COPY . /sec
ENV PYTHONPATH /sec
# install securify requirements
RUN python3.7 -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/  requests   && python3.7 setup.py install  && python3.7 -m pip install --user -r /requirements.txt 

RUN cd /sec/securify/staticanalysis/libfunctors/ && ./compile_functors.sh

RUN cd /sec/securify/staticanalysis/souffle_analysis && \
        souffle --dl-program=../dl-program \
        --fact-dir=/sec/securify/staticanalysis/facts_in \
        --output-dir=/sec/securify/staticanalysis/facts_out \
        -L../libfunctors -w analysis.dl


ENV LD_LIBRARY_PATH /sec/securify/staticanalysis/libfunctors

# Check that everything works and create a cache of the available patterns
# Should be removed
RUN cd /sec/securify/ && securify staticanalysis/testContract.sol

#ENTRYPOINT ["python3.7", "securify/__main__.py"]
